package com.itcast.mallapplet.service.impl;

import com.itcast.admin.vo.OrderAllVO;
import com.itcast.applet.dto.OrderDTO;
import com.itcast.applet.entity.*;
import com.itcast.applet.vo.OrderVO;
import com.itcast.context.BaseContext;
import com.itcast.mallapplet.mapper.*;
import com.itcast.mallapplet.service.OrderService;
import com.itcast.mallapplet.service.ShopEvaluateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private RiderMapper riderMapper;
    @Autowired
    private ShopEvaluateMapper shopEvaluateMapper;

    //新增订单
    @Transactional
    public void orderAdd(OrderDTO orderDTO) {
        //拿到商品数据进行遍历
        List<Dity> goodsList = orderDTO.getGoodsList();
        //拿到订单全部数量
        int number = orderDTO.getGoodsList().stream()
                .mapToInt(Dity::getNumber)
                .sum();
        Double money = 0.0;

        for (Dity dity : goodsList) {
            Commodity commodity = commodityMapper.getById(dity.getId());
            money += commodity.getPrice() * dity.getNumber();
        }

        Order order = Order.builder()
                .userId(BaseContext.getCurrentId())
                .addressId(orderDTO.getAddressId())
                .orderId(String.valueOf(LocalDateTime.now()))
                .number(number)
                .price(money)
                .createTime(LocalDateTime.now())
                .status(1)
                .build();
        //新增订单
        orderMapper.orderAdd(order);

        for (Dity dity : goodsList) {
            Commodity commodity = commodityMapper.getById(dity.getId());
            OrderDity orderDity = OrderDity.builder()
                    .name(commodity.getName())
                    .number(dity.getNumber())
                    .price(commodity.getPrice())
                    .shopId(commodity.getId())
                    .orderId(order.getId())
                    .present(commodity.getPresent())
                    .build();
            //新增下单分表数据
            orderMapper.orderDity(orderDity);
        }
    }

    //查询单个订单：涉及到订单信息，商品信息，骑手信息，送货地址
    @Transactional
    public OrderVO orderQuery(Long id) {
        //先查订单信息表
        Order order = orderMapper.orderGetById(id);
        //查商品信息及其商品分表的图片
        OrderDity orderDity = orderMapper.orderGetByDityId(order.getId());
        //查送货地址
        Address address = addressMapper.queryAddress(order.getAddressId());
        //查询骑手信息
        Rider rider = riderMapper.getById(order.getRiderId());

        OrderVO orderVO = OrderVO.builder()
                .order(order)
                .orderDity(orderDity)
                .address(address)
                .rider(rider)
                .build();

        return orderVO;
    }

    //查询该用户的全部订单
    public List<OrderAllVO> orderQueryAll() {
        List<OrderAllVO> orderAllVOS = orderMapper.orderQueryAll(BaseContext.getCurrentId());
        return orderAllVOS;
    }

    //订单退款
    @Transactional
    public void orderRefund(Long id) {
        Order order = orderMapper.orderGetById(id);
        orderMapper.orderRefund(order);
    }


}
